﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Api.Filter;
using Customer.Bussiness_Service;
using Entity;
using Entity.Sale;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ServiceCore;

namespace Customer.Areas.Sale.Controllers
{
    [ApiController, Area("Sale"), Route("/sale/[action]"), Customer_Admin]
    public class SaleToWorkController : ControllerBase
    { 
        private readonly Isale_service _service;
        private readonly IToole_DI _tool;
        public SaleToWorkController(Isale_service service, IToole_DI tool)
        {
            _service = service;
            _tool = tool;
        }
        #region 主管功能
        /// <summary>
        ///获取销售查询的用户列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel get_sale_userpagelist(get_sale_userpagelist_params query)
        {
            var result = _service.get_sale_userpagelist(query);
            return result;
        } 
        /// <summary>
        /// 销售主管设置用户基础信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel sale_set_user_base(sale_set_user_base_params model)
        {
            var result = _service.sale_set_user_base(model);
            _tool.di_es.Add_Log($"==销售主管设置用户基础信息==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(model)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        } 
        /// <summary>
        ///  分配销售
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel sale_distribution(string userid, string uid)
        {
            var result = _service.sale_distribution(userid, uid);
            _tool.di_es.Add_Log($"==分配销售==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { userid, uid })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        }
        #endregion 
        #region 一般销售页
        /// <summary>
        ///  一般销售查看自己的销售列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel get_sale_myuserpagelist(get_sale_myuserpagelist_params query)
        {
            var result = _service.get_sale_myuserpagelist(query);
            return result;
        } 
        /// <summary>
        ///  新建机器
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel save_mch(create_mch_params model)
        {
            var result = _service.save_mch(model);
            _tool.di_es.Add_Log($"==销售新建机器==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(model)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        }

        /// <summary>
        ///  获取新建机器详情
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel get_work_model(string work_order_id)
        {
            var result = _service.get_work_model(work_order_id);
            return result;
        }

     
        /// <summary>
        ///  生成生产单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel start_work(start_work_params model)
        {
            var result = _service.start_work(model.work_order_ids);
            _tool.di_es.Add_Log($"==销售生成生产单==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(model)}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        }
        /// <summary>
        ///  获取生产单详情
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel get_work_pagelist(get_work_order_details_pagelist_params query)
        {
            var result = _service.get_work_order_details_pagelist(query); 
            return result;
        }  
        /// <summary>
        ///  删除生产单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel del_work(string work_order_id)
        {
            var result = _service.del_work(work_order_id);
            _tool.di_es.Add_Log($"==删除生产单==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { work_order_id })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        }

        /// <summary>
        ///  获取生产单柜体详情
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel get_work_details_model(string work_order_details_id)
        {
            var result = _service.get_work_details_model(work_order_details_id);
            return result;
        }
        #endregion
        #region 销售主管页
        /// <summary>
        ///  销售主管页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultListModel<get_work_order_details_pagelist_showlis> get_manager_workorder_list(get_work_order_details_pagelist_params query)
        {
            var result = _service.get_manager_workorder_list(query);
            return result;
        }
        /// <summary>
        /// 获取销售以及销售主管
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel get_Sale_list(string name)
        {
            var result = _service.get_Sale_list(name);
            return result;
        }
        /// <summary>
        /// 获取工单详情
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel get_work_details_list(string work_order_id)
        {
            var result = _service.get_work_details_list(work_order_id);
            return result;
        } 
        /// <summary>
        /// 审核通过转生产
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel to_work(string work_order_id, string uid)
        {
            var result = _service.to_work(work_order_id, uid);
            _tool.di_es.Add_Log($"==审核通过转生产==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { work_order_id, uid })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        }
        /// <summary>
        /// 更改客户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel change_baseuserid(string work_order_id, string new_baseuserid)
        {
            var result = _service.change_baseuserid(work_order_id, new_baseuserid);
            _tool.di_es.Add_Log($"==更改客户==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { work_order_id, new_baseuserid })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        }

        /// <summary>
        /// 获取用户model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel get_user_mod(string old_baseuserid, string new_baseuserid)
        {
            var result = _service.get_user_mod(old_baseuserid, new_baseuserid);
            return result;
        }

        /// <summary>
        /// 设置工期
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel set_work_end_time(string work_order_id, string work_end_time)
        {
            var result = _service.set_work_end_time(work_order_id, work_end_time);
            _tool.di_es.Add_Log($"==设置工期==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { work_order_id, work_end_time })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        }

        /// <summary>
        /// 退回销售
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel approval_back_work(string work_order_id )
        {
            var result = _service.approval_back_work(work_order_id);
            _tool.di_es.Add_Log($"==退回销售==>请求:{Newtonsoft.Json.JsonConvert.SerializeObject(new { work_order_id })}==>结果:{Newtonsoft.Json.JsonConvert.SerializeObject(result)}", "sale");
            return result;
        }
        /// <summary>
        /// 获取生产主管列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel get_Work_Manager_list(string name)
        {
            var result = _service.get_Work_Manager_list(name);
            return result;
        }

        #endregion
    }
}